﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using InsertDB;

namespace InsertDatabase
{
    public partial class Form1 : Form
    {
        private string strFileName;
        private string connectionString;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileDialog dialog = new OpenFileDialog();
            dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            dialog.InitialDirectory = "C:"; dialog.Title = "Select a text file";
            if (dialog.ShowDialog() == DialogResult.OK)
                strFileName = dialog.FileName;
            if (strFileName == String.Empty)
                return;
            label1.Text = strFileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\DATN\SourceCode\DATN.XMCD\DAL\XMCD.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
            StreamReader sr = new StreamReader(strFileName);
            while (sr.Peek() >= 0)
            {
                string st = sr.ReadLine();
                if (st.Length > 0)
                {
                    listBox1.Items.Add(st.Substring(31, 5) + " -:- " + st);
                    try
                    {
                        Guid? a = new Guid();
                        new xmcdDataContext().sp_DonVi_Insert(st.Substring(31, 5).Trim(), st.Substring(31, 5).Trim(), st, null, null, null, null, null, false, ref a);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(st.Substring(31, 5));
                    }
                }
            }
        }
    }
}